package org.ycx.generator;

import org.apache.velocity.VelocityContext;
import org.ycx.common.util.ClassUtil;
import org.ycx.common.util.VelocityUtil;
import org.ycx.generator.step.ControllerStep;
import org.ycx.generator.step.MapperStep;
import org.ycx.generator.step.ServiceImplStep;
import org.ycx.generator.step.ServiceStep;

import java.io.File;
import java.text.SimpleDateFormat;
import java.util.*;

/**
 * Created by 杨 on 2017-07-18.
 */
public class GeneratorTemplate {

    private IGeneratorStep step;

    private String vmProject;

    /**
     * 生成代码
     *
     * @param module        模块
     * @param targetPackage 默认包名
     * @param models        bean 全名
     * @param commonContext 需要全局加载的上下文
     */
    public void generator(String module, String targetPackage, Set<String> models, Map<String, String> commonContext) throws Exception {
        //当前工程目录
        String vmFile = this.step.getVmFile();
        String project = this.step.getProjectPath(module);
        String currentVM = System.getProperty("user.dir") + File.separator + vmProject + "/src/main/resources/" + vmFile;
        String outPath = project + "/src/main/java/" + targetPackage.replaceAll("\\.", "/") + "/";
        File outPut = new File(outPath);
        if (!outPut.exists()) {
            outPut.mkdirs();
        }
        SimpleDateFormat sf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        for (String modelClass : models) {
            String fileName = step.getOutFileName(modelClass);
            String outputFile = outPath + fileName;
            if (fileName == null) {
                continue;//不生成文件
            } else {
                if (step.overideFile(outputFile)) {//是否覆盖
                    VelocityContext context = new VelocityContext();
                    context = step.initContext(context, modelClass);
                    for (Map.Entry<String, String> ent : commonContext.entrySet()) {
                        context.put(ent.getKey(), ent.getValue());
                    }
                    context.put("target_package", targetPackage);
                    context.put("ctime", sf.format(new Date()));
                    VelocityUtil.generate(currentVM, outputFile, context);
                    System.out.println(outputFile);
                }
            }
        }
        System.out.println("========== 结束生成  ===========");
    }

    public void setStep(IGeneratorStep step) {
        this.step = step;
    }

    public void setVmProject(String vmProject) {
        this.vmProject = vmProject;
    }

    public static void main(String args[]) throws Exception {
//        GeneratorTemplate template = new GeneratorTemplate();
//        template.setStep(new ControllerStep());
//        Map<String, String> context = new HashMap<>();
//        context.put("package_name", "org.ycx.model");
//        template.generator("quick-generator", "org.ddd", ClassUtil.getClassName("org.ycx.model", false), context);
//        //Service
//        template.setStep(new ServiceStep());
//        template.generator("quick-generator", "org.ddd", ClassUtil.getClassName("org.ycx.model", false), context);
//
//
//        template.setStep(new MapperStep());
//        template.generator("quick-generator", "org.ddd.mapper", ClassUtil.getClassName("org.ycx.model", false), context);
//
//        template.setStep(new ServiceImplStep());
//        template.generator("quick-generator", "org.ddd.service.impl", ClassUtil.getClassName("org.ycx.model", false), context);
    }
}
